from django.db import models
from django.contrib.auth.models import User


class CrisisAssessment(models.Model):
    """危机评估记录表"""
    RISK_LEVEL_CHOICES = [
        ('none', '无风险'),
        ('low', '低度风险'),
        ('medium', '中度风险'),
        ('high', '高度风险'),
        ('critical', '极高风险'),
    ]
    
    user = models.ForeignKey(
        User, 
        on_delete=models.CASCADE, 
        verbose_name='用户',
        db_comment='关联用户ID'
    )
    assessment_data = models.JSONField(
        verbose_name='评估数据',
        db_comment='危机评估问答和分数JSON'
    )
    risk_level = models.CharField(
        max_length=20, 
        choices=RISK_LEVEL_CHOICES, 
        verbose_name='风险等级',
        db_comment='评估结果风险等级'
    )
    risk_factors = models.JSONField(
        null=True, 
        blank=True, 
        verbose_name='风险因素',
        db_comment='识别的风险因素JSON'
    )
    intervention_recommended = models.TextField(
        null=True, 
        blank=True, 
        verbose_name='干预建议',
        db_comment='推荐的干预措施'
    )
    is_urgent = models.BooleanField(
        default=False, 
        verbose_name='是否紧急',
        db_comment='是否需要紧急干预'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )

    class Meta:
        db_table = 'xinshi_crisis_assessment'
        db_table_comment = '危机评估记录表'
        verbose_name = '危机评估'
        verbose_name_plural = '危机评估'
        ordering = ['-created_at']

    def __str__(self):
        return f'{self.user.username} - {self.risk_level} - {self.created_at}'


class CrisisIntervention(models.Model):
    """危机干预记录表"""
    STATUS_CHOICES = [
        ('initiated', '已启动'),
        ('in_progress', '进行中'),
        ('resolved', '已解决'),
        ('escalated', '已上报'),
    ]
    
    user = models.ForeignKey(
        User, 
        on_delete=models.CASCADE, 
        verbose_name='用户',
        db_comment='关联用户ID'
    )
    assessment = models.ForeignKey(
        CrisisAssessment, 
        on_delete=models.CASCADE, 
        null=True, 
        blank=True,
        verbose_name='关联评估',
        db_comment='关联的危机评估ID'
    )
    intervention_type = models.CharField(
        max_length=100, 
        verbose_name='干预类型',
        db_comment='干预措施类型'
    )
    description = models.TextField(
        verbose_name='干预描述',
        db_comment='干预过程详细描述'
    )
    status = models.CharField(
        max_length=20, 
        choices=STATUS_CHOICES, 
        default='initiated',
        verbose_name='状态',
        db_comment='干预处理状态'
    )
    resources_provided = models.JSONField(
        null=True, 
        blank=True, 
        verbose_name='提供资源',
        db_comment='提供的帮助资源JSON'
    )
    follow_up_needed = models.BooleanField(
        default=True, 
        verbose_name='需要跟进',
        db_comment='是否需要后续跟进'
    )
    follow_up_date = models.DateTimeField(
        null=True, 
        blank=True, 
        verbose_name='跟进日期',
        db_comment='计划跟进日期'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )
    updated_at = models.DateTimeField(
        auto_now=True, 
        verbose_name='更新时间',
        db_comment='记录更新时间'
    )

    class Meta:
        db_table = 'xinshi_crisis_intervention'
        db_table_comment = '危机干预记录表'
        verbose_name = '危机干预'
        verbose_name_plural = '危机干预'
        ordering = ['-created_at']

    def __str__(self):
        return f'{self.user.username} - {self.intervention_type} - {self.status}'


class HelpResource(models.Model):
    """帮助资源表"""
    RESOURCE_TYPE_CHOICES = [
        ('hotline', '热线电话'),
        ('institution', '专业机构'),
        ('counselor', '咨询师'),
        ('online', '线上资源'),
        ('emergency', '紧急联系'),
    ]
    
    resource_name = models.CharField(
        max_length=200, 
        verbose_name='资源名称',
        db_comment='帮助资源名称'
    )
    resource_type = models.CharField(
        max_length=50, 
        choices=RESOURCE_TYPE_CHOICES, 
        verbose_name='资源类型',
        db_comment='资源类型分类'
    )
    description = models.TextField(
        verbose_name='资源描述',
        db_comment='资源详细介绍'
    )
    contact_info = models.JSONField(
        verbose_name='联系信息',
        db_comment='联系方式JSON(电话/地址/网址)'
    )
    region = models.CharField(
        max_length=100, 
        null=True, 
        blank=True, 
        verbose_name='地区',
        db_comment='资源所在地区'
    )
    available_time = models.CharField(
        max_length=200, 
        null=True, 
        blank=True, 
        verbose_name='服务时间',
        db_comment='资源可用时间'
    )
    is_24_7 = models.BooleanField(
        default=False, 
        verbose_name='全天候服务',
        db_comment='是否提供24小时服务'
    )
    is_active = models.BooleanField(
        default=True, 
        verbose_name='是否启用',
        db_comment='资源是否启用'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )
    updated_at = models.DateTimeField(
        auto_now=True, 
        verbose_name='更新时间',
        db_comment='记录更新时间'
    )

    class Meta:
        db_table = 'xinshi_help_resource'
        db_table_comment = '帮助资源表'
        verbose_name = '帮助资源'
        verbose_name_plural = '帮助资源'

    def __str__(self):
        return self.resource_name
